//
//  CategoryManagerListView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 29.03.25.
//

import SwiftUI

struct CategoryManagerListView: View {
    
    @State var expend = false
    @Binding var categoryListData: Array<AccountingCategory>
    @Binding var subCategoryListData:[String: Array<AccountingCategory>]
    var loadSubCategory:(AccountingCategory)->Void
    var selectedCategoryClick: ((AccountingCategory?) -> Void)? = nil
    var addCategoryClick: ((AccountingCategory?) -> Void)? = nil
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible()),
    ]
    
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(categoryListData) { item in
                    DisclosureGroup (
                        content: {
                            LazyVGrid(columns: columns) {
                                let subCategorys:Array<AccountingCategory>? = subCategoryListData[item.cid!]
                                if (item.hasSubCategory && subCategorys != nil) {
                                    ForEach(subCategorys!) { subItem in
                                        Button(action: {
                                            selectedCategoryClick?(subItem)
                                        }) {
                                            CategoryGridItemView(category: subItem, gridItemBg: GridItemBg.white)
                                        }
                                    }
                                }
                                Button(action: {
                                    addCategoryClick?(item)
                                }) {
                                    CategoryGridItemView(category: .addSubItem, gridItemBg: GridItemBg.white).onAppear {
                                        loadSubCategory(item)
                                    }
                                }
                            }
                        },
                        label: {
                            CategoryManagerItemView(item: item, moreIconClick: { clickItem in
                                // 点击了更多
                                selectedCategoryClick?(clickItem)
                            })
                        })
                        .listRowInsets(EdgeInsets())
                        .listRowSeparator(.hidden)
                        .listRowBackground(Color.clear)
                        .padding(EdgeInsets(top: 10, leading: 14, bottom: 10, trailing: 10))
                        .background(Color.koiColorBackgroundItem)
                        .cornerRadius(16)
                        .disclosureGroupStyle(MyDisclosureStyle())
                }
            }
        }
        
        .listStyle(.plain) // 去除背景色
        .padding(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing:20)) // 去除边距
        .ignoresSafeArea(.container, edges: .bottom)
    }
}

struct MyDisclosureStyle: DisclosureGroupStyle {
    func makeBody(configuration: Configuration) -> some View {
        VStack {
            Button {
                withAnimation {
                    configuration.isExpanded.toggle()
                }
            } label: {
                HStack(alignment: .firstTextBaseline) {
                    configuration.label
                    Spacer()
                }
                .contentShape(Rectangle())
            }
            .buttonStyle(.plain)
            if configuration.isExpanded {
                configuration.content
            }
        }
    }
}

@available(iOS 18.0, *)
#Preview {
    @Previewable @State var datas = Array<AccountingCategory>()
    @Previewable @State var subDatas = [String:Array<AccountingCategory>]()
    CategoryManagerListView(categoryListData: $datas, subCategoryListData: $subDatas, loadSubCategory: { _ in})
}
